package demo1;

public class TestDemo1 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person("xixihaha", 10);
        Person person2 = (Person) person1.clone();
        System.out.println("person1 " + person1);
        System.out.println("person2 " + person2);
        System.out.println("--------------");
        person1.money.m = 20.2;
        person2.money.t.type = "$";
        System.out.println("person1 " + person1);
        System.out.println("person2 " + person2);

    }
    public static void main0(String[] args) {
        String name1 = "Lis";
        String name2 = "sss";
        System.out.println(name2.compareTo(name1));
    }

}

class Money implements Cloneable {
    public double m = 10.2;
    public Type t = new Type();

//    @Override
    protected Object clone() throws CloneNotSupportedException {
        Money money = (Money) super.clone();
        money.t = (Type) t.clone();
        return money;
    }

    @Override
    public String toString() {
        return "{" +
                "m=" + m +
                ", t=" + t +
                '}';
    }
}

class Person implements Cloneable {
    public String name;
    public int age;
    public Money money = new Money();

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", money=" + money +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person person = (Person) super.clone();//已经克隆完成，是一个新的对象
        person.money = (Money) person.money.clone();
        return person;
//        return super.clone() ;//不能仅仅这样写，这是浅拷贝
    }
}


class Type implements Cloneable {
    public String type = "￥";

    @Override
    public String toString() {
        return
                "type='" + type + '\'' ;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}